﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace SAHM
{
    public class Config
    {
        public static GoogleConfig GoogleConfig
        {
            get
            {
                if (_googleConfig == null)
                {
                    _googleConfig = ConfigurationManager.GetSection("SAHM/google") as GoogleConfig;
                }
                return _googleConfig;
            }
        }
        private static GoogleConfig _googleConfig = null;

        public static PiwikConfig PiwikConfig
        {
            get
            {
                if (_piwikConfig == null)
                {
                    _piwikConfig = ConfigurationManager.GetSection("SAHM/piwik") as PiwikConfig;
                }
                return _piwikConfig;
            }
        }
        private static PiwikConfig _piwikConfig = null;

        public static UserVoiceConfig UserVoiceConfig
        {
            get
            {
                if (_userVoiceConfig == null)
                {
                    _userVoiceConfig = ConfigurationManager.GetSection("SAHM/uservoice") as UserVoiceConfig;
                }
                return _userVoiceConfig;
            }
        }
        private static UserVoiceConfig _userVoiceConfig = null;

        public static OwaConfig OwaConfig
        {
            get
            {
                if (_owaConfig == null)
                {
                    _owaConfig = ConfigurationManager.GetSection("SAHM/owa") as OwaConfig;
                }
                return _owaConfig;
            }
        }
        private static OwaConfig _owaConfig = null;

        public static bool IsPiwikEnabled
        {
            get
            {
                if (PiwikConfig == null)
                {
                    return false;
                }
                if (string.IsNullOrEmpty(PiwikConfig.Server))
                {
                    return false;
                }
                if (string.IsNullOrEmpty(PiwikConfig.SiteID))
                {
                    return false;
                }
                return true;
            }
        }

        public static bool IsGoogleEnabled
        {
            get
            {
                if (GoogleConfig == null)
                {
                    return false;
                }
                if (string.IsNullOrEmpty(GoogleConfig.Code))
                {
                    return false;
                }
                return true;
            }
        }
        
        public static bool IsUserVoiceEnabled
        {
            get
            {
                if (UserVoiceConfig == null)
                {
                    return false;
                }
                if (string.IsNullOrEmpty(UserVoiceConfig.SubDomain))
                {
                    return false;
                }
                if (UserVoiceConfig.ForumID <= 0)
                {
                    return false;
                }
                return true;
            }
        }

        public static bool IsOwaEnabled
        {
            get
            {
                if (OwaConfig == null)
                {
                    return false;
                }
                if (string.IsNullOrEmpty(OwaConfig.Server))
                {
                    return false;
                }
                if (string.IsNullOrEmpty(OwaConfig.SiteID))
                {
                    return false;
                }
                return true;
            }
        }

        public static bool IsAnyTrackingSystemEnabled
        {
            get 
            {
                return (IsGoogleEnabled || IsPiwikEnabled || IsUserVoiceEnabled || IsOwaEnabled);
            }
        }
    }


    public class GoogleConfig : ConfigurationSection
    {
        [ConfigurationProperty("Code")]
        public string Code
        {
            get
            {
                return Convert.ToString(this["Code"]??"UNDEFINED");
            }
            set
            {
                this["Code"] = value;
            }
        }
    }

    public class PiwikConfig : ConfigurationSection
    {
        [ConfigurationProperty("Server")]
        public string Server
        {
            get
            {
                return Convert.ToString(this["Server"] ?? "UNDEFINED");
            }
            set
            {
                this["Server"] = value;
            }
        }
        [ConfigurationProperty("SiteID")]
        public string SiteID
        {
            get
            {
                return Convert.ToString(this["SiteID"] ?? "UNDEFINED");
            }
            set
            {
                this["SiteID"] = value;
            }
        }
    }

    public class UserVoiceConfig : ConfigurationSection
    {
        [ConfigurationProperty("SubDomain")]
        public string SubDomain
        {
            get
            {
                return Convert.ToString(this["SubDomain"] ?? "UNDEFINED");
            }
            set
            {
                this["SubDomain"] = value;
            }
        }

        [ConfigurationProperty("ForumID")]
        public int ForumID
        {
            get
            {
                return Convert.ToInt32(this["ForumID"]);
            }
            set
            {
                this["ForumID"] = value;
            }
        }

        [ConfigurationProperty("Alignment")]
        public string Alignment
        {
            get
            {
                string confValue = Convert.ToString(this["Alignment"] ?? "left");
                return string.IsNullOrEmpty(confValue) ? "left" : confValue;
            }
            set
            {
                this["Alignment"] = value;
            }
        }

        [ConfigurationProperty("BackgroundColor")]
        public string BackgroundColor
        {
            get
            {
                string confValue = Convert.ToString(this["BackgroundColor"] ?? "#f00");
                return string.IsNullOrEmpty(confValue) ? "#f00" : confValue;
            }
            set
            {
                this["BackgroundColor"] = value;
            }
        }

        [ConfigurationProperty("TextColor")]
        public string TextColor
        {
            get
            {
                string confValue = Convert.ToString(this["TextColor"] ?? "white");
                return string.IsNullOrEmpty(confValue) ? "white" : confValue;
            }
            set
            {
                this["TextColor"] = value;
            }
        }
        
        [ConfigurationProperty("HoverColor")]
        public string HoverColor
        {
            get
            {
                string confValue = Convert.ToString(this["HoverColor"] ?? "#06c");
                return string.IsNullOrEmpty(confValue) ? "#06c" : confValue;
            }
            set
            {
                this["HoverColor"] = value;
            }
        }

        [ConfigurationProperty("Language")]
        public string Language
        {
            get
            {
                string confValue = Convert.ToString(this["Language"] ?? "en");
                return string.IsNullOrEmpty(confValue) ? "en" : confValue;
            }
            set
            {
                this["Language"] = value;
            }
        }
    }

    public class OwaConfig : ConfigurationSection
    {
        [ConfigurationProperty("Server")]
        public string Server
        {
            get
            {
                return Convert.ToString(this["Server"] ?? "UNDEFINED");
            }
            set
            {
                this["Server"] = value;
            }
        }
        [ConfigurationProperty("SiteID")]
        public string SiteID
        {
            get
            {
                return Convert.ToString(this["SiteID"] ?? "UNDEFINED");
            }
            set
            {
                this["SiteID"] = value;
            }
        }
    }
}
